package com.example.learnspring.interceptor;

import com.example.learnspring.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Component
public class NomoralInterceptorImpl implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("NomoralInterceptorImpl preHandle");

        String token = request.getHeader("Authorization");
        if(token == null){
            response.setStatus(401);
            log.error("NomoralInterceptorImpl preHandle token is null");
            return false;
        }
        try{
            Claims claims = JwtUtil.getClaimsFromToken(token);
            log.info("claims={}",claims);
            return true;
        } catch (Exception e) {
            response.setStatus(401);
            return false;
//            throw new RuntimeException(e);
        }
        //        return super.preHandle(request, response, handler);
    }
}
